package org.pku_uestc.hackathon.handler;

import org.apache.http.client.ResponseHandler;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.pku_uestc.hackathon.CheckService;
import org.pku_uestc.hackathon.bean.ISensorHandler;
import org.pku_uestc.hackathon.sensor.SmartLight;
import org.pku_uestc.hackathon.utils.HTTPRequestHelper;
import org.pku_uestc.hackathon.utils.PrefUtils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

@SuppressLint("HandlerLeak")
public class HighHumHandler implements ISensorHandler {

	private float threshold = 20;
	private Context context;
	
	public HighHumHandler(Context context){
		this.context = context;
	}
	
	@SuppressLint("HandlerLeak")
	private final Handler weatherHandler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			
			JSONTokener jsonParser = new JSONTokener(msg.getData().getString("RESPONSE"));  
			try {
				JSONObject data = (JSONObject) jsonParser.nextValue();
				if(data.getJSONObject("weatherinfo").getString("weather1").contains("雨")){
					//if rain
					
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
	 			e.printStackTrace();
			}  
		}
	};
	
    @Override
    public boolean onAction() {
        // TODO Auto-generated method stub
    	Log.d(getClass().getName(), "action");
    	
    	if(PrefUtils.getBooleanValue(context, CheckService.phone_pre
				+getType(), false)){
    		
    	}
    	
    	String s = PrefUtils.getStringValue(context, CheckService.light_pre
				+getType(), "");
    	if(!s.equals("")){
    		String[] ids = s.split(" ");
    		for(String id:ids)
    			SmartLight.getInstantces(Integer.parseInt(id)).action();
    	}
    	
        return false;
    }
    
    protected void performGetRequest(Handler handler, final String url){ 
        final ResponseHandler<String> responseHandler = 
                        HTTPRequestHelper.getResponseHandlerInstance(handler);
        new Thread(){
                @Override 
                public void run(){ 
                        HTTPRequestHelper helper=new HTTPRequestHelper(responseHandler); 
                        helper.performGet(url, null, null, null);
                } 
        }.start(); 
    }

    @Override
    public boolean check(Object... datas) {
        // TODO Auto-generated method stub
    	if(Float.parseFloat(datas[1].toString())>threshold){
    		onAction();
    	}else{
    		performGetRequest(weatherHandler,"http://m.weather.com.cn/data/101010200.html");
    	}
        return false;
    }

	@Override
	public int getType() {
		// TODO Auto-generated method stub
		return TYPE_HIGHHUMHANDLER;
	}
	
	@Override
	public void changeThreshold(float threshold) {
		// TODO Auto-generated method stub
		this.threshold = threshold;
	}

}
